package com.playball.lottery.frm.yhzx;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.base.BaseActivity;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.nt.GetRequestParams;


/**
 * 安全中心 - 安全问题
 * 
 * @author tanglizhong
 * @version 2014-3-26 上午10:37
 */
public class ScQuestionActivity extends BaseActivity {

	/** 标题栏 */
	private TextView info;

	/**
	 * 设置安全问题布局
	 */
	private View view_sc_question_q;
	/** 安全问题下拉 */
	private Spinner yhzx_sc_spn_question;
	/** 自定义 */
	private View view_sc_question_custom;
	/** 自定义问题 */
	private EditText yhzx_sc_et_question_custom;
	/** 问题答案 */
	private EditText yhzx_sc_et_answer;
	/** 提交按钮 */
	private Button yhzx_sc_btn_submit;

	/**
	 * 重设安全问题布局
	 */
	private View view_sc_question_change;
	/** 原安全问题 */
	private TextView yhzx_sc_tv_question_old;
	/** 原安全答案 */
	private EditText yhzx_sc_et_answer_old;
	/** 新安全问题下拉 */
	private Spinner yhzx_sc_spn_question_new;
	/** 新自定义 */
	private View view_sc_question_custom_new;
	/** 新自定义问题 */
	private EditText yhzx_sc_et_question_custom_new;
	/** 新安全问题答案 */
	private EditText yhzx_sc_et_answer_new;
	/** 新安全问题提交按钮 */
	private Button yhzx_sc_btn_submit_new;

	private String[] questions;
	private ArrayAdapter<String> questionsAdapter;
	private String currQuestion = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.yhzx_sc_question);

		initView();
		titleBar();
		initData();
		bindSpinner();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if ("".equals(ApplicationData.getInstance().getSecurityQuestion())) {
			view_sc_question_q.setVisibility(View.VISIBLE);
			view_sc_question_change.setVisibility(View.GONE);
		} else {
			view_sc_question_q.setVisibility(View.GONE);
			view_sc_question_change.setVisibility(View.VISIBLE);
		}
		
	}

	public void onPause() {
		super.onPause();
		
	}

	/**
	 * 绑定安全问题下拉数据
	 */
	private void bindSpinner() {
		questionsAdapter = new ArrayAdapter<String>(this, R.layout.sc_spinner_item, questions);
		questionsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		//
		yhzx_sc_spn_question.setAdapter(questionsAdapter);
		yhzx_sc_spn_question.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				if (position == questions.length - 1) {
					view_sc_question_custom.setVisibility(View.VISIBLE);
				} else {
					view_sc_question_custom.setVisibility(View.GONE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});

		yhzx_sc_spn_question_new.setAdapter(questionsAdapter);
		yhzx_sc_spn_question_new.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				if (position == questions.length - 1) {
					view_sc_question_custom_new.setVisibility(View.VISIBLE);
				} else {
					view_sc_question_custom_new.setVisibility(View.GONE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		info.setText("设置/修改安全问题");
		currQuestion = ApplicationData.getInstance().getSecurityQuestion();
		yhzx_sc_tv_question_old.setText(currQuestion);
		questions = getResources().getStringArray(R.array.security_question);
	}

	/**
	 * 初始化组件
	 */
	private void initView() {
		info = (TextView) findViewById(R.id.top_title_info);

		view_sc_question_q = findViewById(R.id.view_sc_question_q);
		yhzx_sc_spn_question = (Spinner) findViewById(R.id.yhzx_sc_spn_question);
		view_sc_question_custom = findViewById(R.id.view_sc_question_custom);
		yhzx_sc_et_question_custom = (EditText) findViewById(R.id.yhzx_sc_et_question_custom);
		yhzx_sc_et_answer = (EditText) findViewById(R.id.yhzx_sc_et_answer);
		yhzx_sc_btn_submit = (Button) findViewById(R.id.yhzx_sc_btn_submit);
		yhzx_sc_btn_submit.setOnClickListener(btn_onclick);

		view_sc_question_change = findViewById(R.id.view_sc_question_change);
		yhzx_sc_tv_question_old = (TextView) findViewById(R.id.yhzx_sc_tv_question_old);
		yhzx_sc_et_answer_old = (EditText) findViewById(R.id.yhzx_sc_et_answer_old);
		yhzx_sc_spn_question_new = (Spinner) findViewById(R.id.yhzx_sc_spn_question_new);
		view_sc_question_custom_new = findViewById(R.id.view_sc_question_custom_new);
		yhzx_sc_et_question_custom_new = (EditText) findViewById(R.id.yhzx_sc_et_question_custom_new);
		yhzx_sc_et_answer_new = (EditText) findViewById(R.id.yhzx_sc_et_answer_new);
		yhzx_sc_btn_submit_new = (Button) findViewById(R.id.yhzx_sc_btn_submit_new);
		yhzx_sc_btn_submit_new.setOnClickListener(btn_onclick);

	}

	private View.OnClickListener btn_onclick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			JSONObject jsob = new JSONObject();
			try {
				switch (v.getId()) {
				case R.id.yhzx_sc_btn_submit:
					int spnIdx = yhzx_sc_spn_question.getSelectedItemPosition();
					if (spnIdx == 0) {
						showTip(questions[0]);
						return;
					}
					jsob.put("oldanswer", "");
					jsob.put("newanswer", yhzx_sc_et_answer.getText().toString());
					if (spnIdx == questions.length - 1) {
						currQuestion = yhzx_sc_et_question_custom.getText().toString();
					} else {
						currQuestion = questions[spnIdx];
					}
					jsob.put("question", currQuestion);
					break;
				case R.id.yhzx_sc_btn_submit_new:
					int spnIdxNew = yhzx_sc_spn_question_new.getSelectedItemPosition();
					if (spnIdxNew == 0) {
						showTip(questions[0]);
						return;
					}
					jsob.put("oldanswer", yhzx_sc_et_answer_old.getText().toString());
					jsob.put("newanswer", yhzx_sc_et_answer_new.getText().toString());
					if (spnIdxNew == questions.length - 1) {
						currQuestion = yhzx_sc_et_question_custom_new.getText().toString();
					} else {
						currQuestion = questions[spnIdxNew];
					}
					jsob.put("question", currQuestion);
					break;
				}
			} catch (JSONException ex) {
				return;
			}
			if (currQuestion.length() < 1) {
				showTip("自定义问题不能为空");
				return;
			}
			QuestionTask task = new QuestionTask();
			String[] params = new String[2];
			params[0] = "121";
			params[1] = jsob.toString();
			task.execute(params);
		}
	};

	/** TitleBar标题栏 */
	public void titleBar() {
		info.setText(R.string.yhzx_sc_question);
		TextView back = (TextView) findViewById(R.id.top_title_back);
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	private class QuestionTask extends AsyncTask<String, Integer, String> {
		ShowTipDialog showdialog = new ShowTipDialog();

		@Override
		protected void onPreExecute() {
			showdialog.showDialog(ScQuestionActivity.this);
		}

		@Override
		protected String doInBackground(String... params) {
			String action = params[0];
			String strResult = null;

			// 获取请求返回值
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(params[1], UserID, action, getApplicationContext());
			return strResult;
		}

		@Override
		protected void onPostExecute(String result) {
			int code = -1;
			try {
				if (null != result) {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					code = js.getInt("Code");
					if (code == 0) {
						ApplicationData.getInstance().setSecurityQuestion(currQuestion);
					}
					String tip = js.getString("Data");
					showTip(tip);
				}
			} catch (JSONException e) {
				// e.printStackTrace();
			} finally {
				showdialog.closeDialog();
				if (code == 0) {
					ScQuestionActivity.this.finish();
				}
			}
		}

	}

	/** 显示文字提示 */
	private void showTip(String tip) {
		Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
	}
}
